package com.mekegi.whoanthro;

import android.content.ContentValues;
import android.database.Cursor;
import java.util.Date;
import java.util.HashMap;

/**
 * @since 23.02.13 15:16
 * @author Arsen Abdusalamov <duburlan@gmail.com>
 */
public class Child extends AbstractModel implements IModel{

    protected String mFirstNameString;
    protected String mLastNameString;
    protected String mOtherNameString;
    protected Date mBirthdayDate;

    public static final String TABLE_NAME = "childs";
    
    public static final String KEY_FIRST_NAME = "first_name";
    public static final String KEY_LAST_NAME = "last_name";
    public static final String KEY_OTHER_NAME = "other_name";
    public static final String KEY_BIRTHDAY = "birthday";

    protected static final HashMap<String, Integer> COLUMNS_MAP;
    static {
        HashMap<String, Integer> aMap = new HashMap<String, Integer>();
        aMap.put(KEY_ID, FIELD_TYPE_INTEGER);
        aMap.put(KEY_FIRST_NAME, FIELD_TYPE_STRING);
        aMap.put(KEY_LAST_NAME, FIELD_TYPE_STRING);
        aMap.put(KEY_OTHER_NAME, FIELD_TYPE_STRING);
        aMap.put(KEY_BIRTHDAY, FIELD_TYPE_INTEGER);
        COLUMNS_MAP = aMap;
    }




//{{{<editor-fold defaultstate="collapsed" desc="getter setter">
    public HashMap<String, Integer> getColumnsMap() {
        return COLUMNS_MAP;
    }

    public final String getFirstName() {
        return mFirstNameString;
    }

    public final void setFirstName(String mFirstNameString) {
        this.mFirstNameString = mFirstNameString;
    }

    public final String getLastName() {
        return mLastNameString;
    }

    public final void setLastName(String mLastNameString) {
        this.mLastNameString = mLastNameString;
    }

    public final String getOtherName() {
        return mOtherNameString;
    }

    public final void setOtherName(String mOtherNameString) {
        this.mOtherNameString = mOtherNameString;
    }

    public final Date getBirthday() {
        return mBirthdayDate;
    }

    public final void setBirthdayDate(Date mBirthdayDate) {
        this.mBirthdayDate = mBirthdayDate;
    }

    public final void setBirthdayDate(Long mills) {
        this.mBirthdayDate = new Date(mills);
    }
//}}}</editor-fold>

//{{{<editor-fold defaultstate="collapsed" desc="constructors">

    public Child() {
    }

    public Child(Long id) {
        super(id);
    }

    public Child(String mFirstNameString, String mLastNameString, Date mBirthdayDate) {
        this.setFirstName(mFirstNameString);
        this.setLastName(mLastNameString);
        this.setBirthdayDate(mBirthdayDate);
    }

    public Child(String mFirstNameString, String mLastNameString, String mOtherNameString, Date mBirthdayDate) {
        this.setFirstName(mFirstNameString);
        this.setLastName(mLastNameString);
        this.setBirthdayDate(mBirthdayDate);
        this.setOtherName(mOtherNameString);
    }

    public Child(Long id, String mFirstNameString, String mLastNameString, String mOtherNameString, Date mBirthdayDate) {
        super(id);
        this.mFirstNameString = mFirstNameString;
        this.mLastNameString = mLastNameString;
        this.mOtherNameString = mOtherNameString;
        this.mBirthdayDate = mBirthdayDate;
    }

    public Child(Long id, String mFirstNameString, String mLastNameString, Date mBirthdayDate) {
        super(id);
        this.mFirstNameString = mFirstNameString;
        this.mLastNameString = mLastNameString;
        this.mBirthdayDate = mBirthdayDate;
    }

//}}}</editor-fold>

    /**
     * @since 23.02.13 15:35
     * @author Arsen Abdusalamov <duburlan@gmail.com>
     * @return String
     */
    public String getFullName() {
        String result = getFirstName() + " " + getLastName();
        if (null != getOtherName() && !getOtherName().isEmpty()) {
            result += " " + getOtherName();
        }
        return result;
    }

    @Override
    public String toString() {
        String result = "Child: " + getFullName();
        if( null != getBirthday()) {
            result += "\n from: " + getBirthday().toLocaleString();
        }
        return result;
    }

    @Override
    public void fromContentValues(ContentValues contentValues) {
        super.fromContentValues(contentValues);

        if (contentValues.containsKey(KEY_FIRST_NAME)) {
            this.setFirstName(contentValues.getAsString(KEY_FIRST_NAME));
        }
        if (contentValues.containsKey(KEY_LAST_NAME)) {
            this.setLastName(contentValues.getAsString(KEY_LAST_NAME));
        }
        if (contentValues.containsKey(KEY_OTHER_NAME)) {
            this.setOtherName(contentValues.getAsString(KEY_OTHER_NAME));
        }
        if (contentValues.containsKey(KEY_BIRTHDAY)) {
            this.setBirthdayDate(contentValues.getAsLong(KEY_BIRTHDAY));
        }
    }

    @Override
    public ContentValues toContentValues() {
        ContentValues result = super.toContentValues();

        result.put(KEY_ID, getId());
        result.put(KEY_FIRST_NAME, getFirstName());
        result.put(KEY_LAST_NAME, getLastName());
        result.put(KEY_OTHER_NAME, getOtherName());
        result.put(KEY_BIRTHDAY, getBirthday().getTime());

        return result;
    }

    @Override
    public String getTextForView() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
